<template>
  <div class="app-container">
    <el-button v-if="checkPermission(permissionRoles.device_add)" type="primary" @click="handleAdd">添加设备码</el-button>
    <el-button v-if="checkPermission(permissionRoles.device_batch_del)" type="danger" @click="batchDelete">批量删除</el-button>
    <el-switch
      v-model="deviceSwitch"
      active-text="限制设备码"
      inactive-text="不限制"
      active-value="0"
      inactive-value="1"
      @change="handleSwitch"
    >
    </el-switch>

    <el-table v-loading="listLoading" :data="list" class="mt-30" height="705" border @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50"></el-table-column>
      <el-table-column label="设备码" prop="code" align="center"></el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button v-if="checkPermission(permissionRoles.device_del)" type="text" size="small" title="删除" @click="handleDelete(scope)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :visible.sync="dialogVisible" :title="textMap[dialogType]" :close-on-click-modal="false">
      <el-form ref="dataForm" :model="dataForm" :rules="rules" label-width="100px" label-position="left">
        <el-form-item label="设备码" prop="code">
          <el-input v-model="dataForm.code" class="defaut-input" placeholder="设备码" />
        </el-form-item>
      </el-form>
      <div class="tr">
        <el-button type="danger" @click="dialogVisible=false">取消</el-button>
        <el-button type="primary" @click="confirmSubmit()">确定</el-button>
      </div>
    </el-dialog>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getDeviceList" />
  </div>
</template>
<script>
import { getDeviceList, deleteDeviceCode, handleDevice, batchDeleteDevice, handleSwithDevice } from '@/api/ops'
import checkPermission from '@/utils/permission'
import Pagination from '@/components/Pagination'

const defaultItem = {
  id: '',
  code: '',
  page: 1,
  limit: 200
}
export default {
  name: 'DeviceCode',
  components: { Pagination },
  data() {
    return {
      permissionRoles: this.$store.state.user.permissionRoles,
      listLoading: false,
      dataForm: {
        id: '',
        code: ''
      },
      rules: {
      },
      listQuery: {
        page: 1,
        limit: 200
      },
      multipleSelection: [],
      total: 0,
      deviceSwitch: '0',
      list: [],
      dialogVisible: false,
      dialogType: 'new',
      textMap: {
        new: '新增'
      }
    }
  },
  computed: {
  },
  created() {
    this.getDeviceList()
  },
  methods: {
    checkPermission,
    async getDeviceList() {
      this.listLoading = true
      getDeviceList(this.listQuery).then(response => {
        this.list = response.data.items
        this.total = response.data.total
        this.deviceSwitch = response.data.switch
        this.listLoading = false
      })
    },
    handleAdd() {
      this.dataForm = Object.assign({}, defaultItem)
      this.dialogType = 'new'
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    handleDelete({ $index, row }) {
      this.$confirm('你确定要删除此列吗?', '删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          await deleteDeviceCode(row.id)
          this.list.splice($index, 1)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(err => { console.error(err) })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    batchDelete({ $index, row }) {
      this.$confirm('你确定要批量删除数据吗?', '删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          const ids = []
          for (let i = 0; i < this.multipleSelection.length; i++) {
            ids.push(this.multipleSelection[i].id)
          }
          await batchDeleteDevice(ids)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getDeviceList()
        })
        .catch(err => { console.error(err) })
    },
    confirmSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          handleDevice(this.dataForm).then(response => {
            if (response.status) {
              this.dialogVisible = false
              this.$message({
                type: 'success',
                message: '操作成功!'
              })
              this.getDeviceList()
            } else {
              this.message.error(response.message)
            }
          })
        }
      })
    },
    handleSwitch(val) {
      this.$confirm('你确定要操作限制设备码吗?', '限制设备码', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          handleSwithDevice(val).then(response => {
            this.$message({
              type: 'success',
              message: '操作成功!'
            })
            this.getDeviceList()
          })
        })
        .catch(err => { console.error(err) })
    }
  }
}
</script>
<style scoped>
.el-table {
  font-size: 14px;
}
.el-table td, .el-table th {
  padding: 10px 0;
}
.el-table th>.cell {
  padding-left: 10px;
  padding-right: 10px;
}
</style>
